<?php

require_once 'BaseHelper.class.php';

class ReserveModuleHelper extends BaseHelper {
	
	public static $MODULE_NAME = "ReserveModule"; //数据库操作的模块名

	//------------------------------------基本函数开始----------------------------------------
	public static function add($data,$displaySQL = false) {
		return parent::add ( self::$MODULE_NAME, $data,$displaySQL );
	}
	public static function update($data, $whereStr="",$displaySQL = false) {
		return parent::update ( self::$MODULE_NAME, $data, $whereStr,$displaySQL );
	}
	public static function updateSQL($sql,$displaySQL = false) {
		return parent::updateSQL( self::$MODULE_NAME, $sql,$displaySQL);
	}
	public static function delete($whereStr="",$displaySQL = false) {
		return parent::delete ( self::$MODULE_NAME, $whereStr ,$displaySQL);
	}
	public static function getObject($whereStr="",$displaySQL = false) {
		return parent::getObject ( self::$MODULE_NAME, $whereStr ,$displaySQL);
	}
	public static function queryObject($sql,$displaySQL = false) {
		return parent::queryObject ( self::$MODULE_NAME, $sql ,$displaySQL);
	}
	public static function queryForInt($sql,$displaySQL = false) {
		return parent::queryForInt ( self::$MODULE_NAME, $sql ,$displaySQL);
	}
	public static function getList($whereStr = '', $orderStr = '', $limitNum = 0,$displaySQL = false) {
		return parent::getList ( self::$MODULE_NAME, $whereStr, $orderStr, $limitNum ,$displaySQL);
	}
	public static function queryList($sql = '', $displaySQL = false) {
		return parent::queryList(self::$MODULE_NAME,$sql,$displaySQL);
	}
	//------------------------------------基本函数结束----------------------------------------


	//------------------------------------自定义函数开始--------------------------------------
	
	/**
	 * 添加【事务控制】
	 */
	public static function addWithTrans($data) {
		
		//1....增加一个ReserveModule
		//2....reserve_module_field表进行相应的记录
		
		$model = M();
		$model->startTrans();
		
		//1....增加一个ReserveModule
		$resultid = ReserveModuleHelper::add($data);
		
		//2....reserve_module_field表进行相应的记录【记录系统字段 timeline和id】
		
		//记录id字段
		$data2["reserve_module_id"] = $resultid;
		$data2["field_name"] = "id";
		$data2["field_desc"] = 'ID';
		$data2["form_type"] = FormType::$NUMBER;
		$data2["is_system"] = 1;
		$data2["order_num"] = 88888;
		$result2 = ReserveModuleFieldHelper::add($data2);
		
		//记录timeline字段
		$data3["reserve_module_id"] = $resultid;
		$data3["field_name"] = "timeline";
		$data3["field_desc"] = '发布时间';
		$data3["form_type"] = FormType::$NUMBER;
		$data3["is_system"] = 1;
		$data3["order_num"] = 0;
		$result3 = ReserveModuleFieldHelper::add($data3);
		
		//记录catalog_id字段
		$data4["reserve_module_id"] = $resultid;
		$data4["field_name"] = "catalog_id";
		$data4["field_desc"] = '栏目ID';
		$data4["form_type"] = FormType::$NUMBER;
		$data4["is_system"] = 1;
		$data4["order_num"] = 0;
		$result4 = ReserveModuleFieldHelper::add($data4);
		
		//记录reserve_status字段 [0未处理/1已处理]
		$data5["reserve_module_id"] = $resultid;
		$data5["field_name"] = "reserve_status";
		$data5["field_desc"] = '状态(0未处理 / 1已处理)';
		$data5["form_type"] = FormType::$NUMBER;
		$data5["is_system"] = 1;
		$data5["order_num"] = 0;
		$result5 = ReserveModuleFieldHelper::add($data5);
		
		if($resultid !== false && $result2 !== false && $result3 !== false && $result4 !== false && $result5 !== false){
			//DB COMMIIT
			$model->commit();
			return true;
		}else{
			$model->rollback();
			return false;
		}
		
	}
	
	
	/**
	 * 删除【事务控制】
	 */
	public static function deleteWithTrans($id) {
		
		$model = M();
		$model->startTrans();
		
		//1....ReserveModule删除
		$result1 = ReserveModuleHelper::delete("reserve_module_id={$id}");
		
		//2....reserve_module_field表相应记录清除
		$result2 = ReserveModuleFieldHelper::delete("reserve_module_id={$id}");
		
		if($result1 !== false && $result2 !== false){
			//DB COMMIIT
			$model->commit();
			return true;
		}else{
			$model->rollback();
			return false;
		}
		
	}
	
	//------------------------------------自定义函数结束--------------------------------------


}